{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "be57b2b3-c3d8-41f5-9a70-ed45eea6e2f6",
   "metadata": {},
   "source": [
    "# python 基础"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "abe4b76a-a687-4b9a-9d3d-57c9954f1bfe",
   "metadata": {},
   "source": [
    "数据类型"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "be6b8079-3655-45c9-833a-33bc762a8028",
   "metadata": {},
   "source": [
    "- 数字  类型 int float  其他复数  我们几乎用不到 因此没有记录"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a0553fca-d930-4f11-80fd-90a3908d9a56",
   "metadata": {
    "tags": []
   },
   "source": [
    "既然是数字 那么 基本的运算符就是支持的 加减乘除 括号 平方 开平方 绝对值 ... "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "9f333c07-0fce-41b2-99d6-d7463592cba9",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "x = 1\n",
    "y = 2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "95ef0338-fb3c-4116-a669-6197ebde609b",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1\n",
      "1.0\n"
     ]
    }
   ],
   "source": [
    "x = int(x)      # 将x转换为一个整数。（常用）\n",
    "print(x)\n",
    "x = float(x)# 将x转换到一个浮点数。  (常用)\n",
    "print(x)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "e7195eb6-f058-4123-86d1-35eae5eba220",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "a,b = 5,2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "d9695cd8-67bb-4b35-af24-fe871b6c2334",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "int"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "type(a)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "06009617-6ae9-480b-a2c9-b5d29d29ef8b",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "7"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a + b"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "id": "210c1421-957c-4a0f-9850-94640675ebc4",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "3"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a - b"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "id": "4cc4efe7-2475-4c3d-bec6-a95803c68f31",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "10"
      ]
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a * b"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "id": "1fbf044b-8495-4767-8ed6-761d83a0fee0",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2.5"
      ]
     },
     "execution_count": 28,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a / b"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "id": "520e9ba1-52c5-4aea-9ed9-700f53b71b19",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "int"
      ]
     },
     "execution_count": 32,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "type(a//b)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "id": "4818e19e-a506-4b97-af52-6878f02a5634",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2"
      ]
     },
     "execution_count": 30,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a // b"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "id": "7335c735-19f1-4294-857c-8b553aec8616",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "3"
      ]
     },
     "execution_count": 34,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "abs(2-5)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 208,
   "id": "6e069a5a-b249-4ac1-a19a-f2a8870275c3",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "25"
      ]
     },
     "execution_count": 208,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pow(a,b)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 210,
   "id": "13a73259-43c2-48fc-b471-ce01246ac6e7",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "25"
      ]
     },
     "execution_count": 210,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a ** b"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f43a9bc6-5c20-4109-a692-fe5c21729195",
   "metadata": {},
   "source": [
    "整数有的 浮点类型也有 浮点类型 还可以 向上 向下取整 选择保留小数 位数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 54,
   "id": "185fded5-d004-4139-bee1-3614a484c772",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "c,d = 3.1415926,2.7"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "id": "db37976a-5d86-4f37-bb44-412a0567538d",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "5.8415926"
      ]
     },
     "execution_count": 40,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "c+d"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "id": "dcbb381a-e743-4398-93d7-e78d5f610a6e",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.4415925999999999"
      ]
     },
     "execution_count": 42,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "c- d"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "id": "2d87335e-706b-4154-8c79-da959ff9ea4c",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "8.48230002"
      ]
     },
     "execution_count": 44,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "c*d"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "id": "80f6087c-c905-4561-81ab-9c9693bbf685",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1.1635528148148149"
      ]
     },
     "execution_count": 46,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "c/d"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 48,
   "id": "3f481327-a13e-434d-8ea6-d7c932fc4fd0",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1.0"
      ]
     },
     "execution_count": 48,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "c // d"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 50,
   "id": "7164315e-4bfc-4e0a-bf1a-a1f7e1975938",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "3.14"
      ]
     },
     "execution_count": 50,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "round(c,2)  # 保留小数位数 会四舍五入"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 226,
   "id": "756d2e7e-dbe7-40aa-aed3-68df5c4f3429",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "3"
      ]
     },
     "execution_count": 226,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "round(d)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 58,
   "id": "22a6b7ef-7ce7-4793-aed1-febe7d17cd1d",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "import math"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 228,
   "id": "bcf777af-ff8b-4bfb-8cee-1c0bfae30bc1",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "3"
      ]
     },
     "execution_count": 228,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "math.floor(c)  # 向下取整"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 230,
   "id": "76c64eb3-697f-47ff-80ee-12d11345f98d",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2"
      ]
     },
     "execution_count": 230,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "math.floor(d)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 232,
   "id": "6f7ace5a-ea21-45c5-92e4-33497a34b359",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "4"
      ]
     },
     "execution_count": 232,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "math.ceil(c)   # 向上取整   "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 60,
   "id": "6a162497-ba06-4d48-81fc-3eceab728e30",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "3"
      ]
     },
     "execution_count": 60,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "math.ceil(d)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 236,
   "id": "42f254b6-dcd4-4699-8128-e223829ae4b8",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "5"
      ]
     },
     "execution_count": 236,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "max(a,b,c,d)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 238,
   "id": "19bae194-0ebc-4319-a571-b9784901df24",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2"
      ]
     },
     "execution_count": 238,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "min(a,b,c,d)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "f819fe57-bc04-40a8-bd03-7b626eb02da8",
   "metadata": {},
   "outputs": [],
   "source": [
    "还有一个随机函数 可以随机生成数字"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 62,
   "id": "22eed47d-60b8-4dc8-b9de-683532c25901",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "import random"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 89,
   "id": "d2b9f027-dbbb-4825-ac58-275652527144",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.6174207962281751"
      ]
     },
     "execution_count": 89,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "random.random()   # 0-1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 87,
   "id": "511d5fcf-7f67-4ee5-88b6-b58505adc263",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1"
      ]
     },
     "execution_count": 87,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "random.randint(1,10)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a7d80b8a-800c-46e1-830b-497c745e8d63",
   "metadata": {
    "tags": []
   },
   "source": [
    "---"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "66da2910-ecc0-4b47-8df1-fed43f07c4c7",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "False"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 注意 浮点类型是不精确的\n",
    "0.1 + 0.1 +0.1 == 0.3   # if \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 93,
   "id": "3382a91d-0516-4cdd-8d71-956464167ffa",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.3"
      ]
     },
     "execution_count": 93,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "0.3"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 103,
   "id": "821842d3-8447-4e62-9b1a-88292fb39eaa",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.2"
      ]
     },
     "execution_count": 103,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "(0.1+0.1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 111,
   "id": "cdd23ddf-78b1-45d8-9ef0-86bf1e7a6809",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 111,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "0.2 + 0.2 == 0.4"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "03395532-33cc-49f7-a6e8-b3f963f5205a",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
